/**
 * MOTOR 2 - RAPID FLASH DYNAMICS
 * Copyright (c) 2007-2008, Michael Baczynski
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * * Neither the name of the polygonal nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without specific
 *   prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package de.polygonal.motor2.collision.shapes
{
	import de.polygonal.motor2.math.V2;	

	public class ShapeLib
	{
		public static function ngon(sides:uint, xradius:uint, yradius:uint = 0):Array
		{
			var span:Number, i:int, verts:Array = new Array();
			
			if (xradius == 0 || sides < 3) return null;
			if (yradius == 0) yradius = xradius;
			
			span = Math.PI * 2 / sides;
			for (i = 0; i < sides; i++)
			{
				verts.push(Math.cos(i * span) * xradius);
				verts.push(Math.sin(i * span) * yradius);
			}
			return verts;
		}
		
		public static function box(w:uint, h:uint):Array
		{
			if (w == 0 || h == 0) return null;
			
			var w2:Number = w / 2;
			var h2:Number = h / 2;
			
			return [
				new V2(-w2, -h2),
				new V2( w2, -h2),
				new V2( w2,  h2),
				new V2(-w2,  h2)
			];
		}
		
		public static function capsule(h:uint, w0:uint, w1:uint, seg0:uint = 3, seg1:uint = 3):Array
		{
			var t1:Number = w0 / 2;
			var t2:Number = w1 / 2;
			var h2:Number = (h - t1 - t2) * .5;
			
			var span0:Number = Math.PI / (seg0 + 1);
			var span1:Number = Math.PI / (seg1 + 1);
			
			var verts:Array = [], p:V2, k:int = 0, i:int = 0;
			
			k = seg0 + 1;
			for (i = 0; i <= k; i++)
			{
				p = new V2(Math.cos(i * span0) * t1, Math.sin(i * span0) * t1 + h2);
				verts[k++] = p;
			}
			
			k = seg1 + 1;
			for (i = 0; i <= k; i++)
			{
				p = new V2(-Math.cos(i * span1) * t2, -Math.sin(i * span1) * t2 - h2);
				verts[k++] = p;
			}
			
			return verts;
		}
	}
}
